<%--
  Created by IntelliJ IDEA.
  User: Li Chuanwei
  Date: 2023-07-15
  Time: 19:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <head>
        <title>轮播图管理</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
        <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
        <script src="${pageContext.request.contextPath}/static/layui/layui.js" charset="utf-8"></script>
        <script src="${pageContext.request.contextPath}/static/js/showBigImage.js" charset="utf-8"></script>
        <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/public.css" media="all">
    </head>
    <style>
        .layui-table-cell {
            text-align: center;
            height: auto !important;
            white-space: normal;
        }
    </style>
</head>
<body>
<div class="layuimini-container ">
    <div class="layuimini-main">

        <%-- 表头工具条--%>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <a class="layui-btn layui-btn-normal layui-btn-sm data-count-edit" lay-event="addCarousel">添加轮播图</a>
                <a class="layui-btn layui-btn-danger layui-btn-sm data-count-edit" lay-event="batchDelist">批量删除</a>
            </div>
        </script>

        <%-- 轮播图图片列--%>
        <script type="text/html" id="carouselPic">
            <img src="${IMG_SERVER}/carousel/{{d.pic}}" onclick="showBigImage(this)" style="width: 100% ; height: 100%"
                 class="carouselPic">
        </script>

        <%-- 启用状态开关按钮--%>
        <script type="text/html" id="shelfStateSwitch">
            <input type="checkbox" name="status" value="{{= d.id }}" title="已启用|已停用" lay-skin="switch"
                   lay-filter="Switch"
                   {{# if (d.state== 1) { }}
                   checked
                   {{# } }}>
        </script>

        <%-- 货物列表--%>
        <table class="layui-hide" id="carouselTable" lay-filter="carouselTableFilter"></table>

        <%-- 行内操作工具条--%>
        <script type="text/html" id="carouselTableBar">
            <a class="layui-btn layui-btn-danger layui-btn-sm data-count-edit" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table', 'element', 'layer'], function (message) {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        dropdown = layui.dropdown;

        /**
         * 表格渲染
         */
        table.render({
            elem: '#carouselTable',
            autoSort: true,
            request: {
                pageName: 'pageNum', // 页码的参数名称，默认：page
                limitName: 'pageSize' // 每页数据条数的参数名，默认：limit
            },
            url: '${pageContext.request.contextPath}/manager/carousel/list',
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox"},
                {title: '序号', type: "numbers", align: "center"},
                {field: 'id',  title: '轮播图编号', sort: true,hide:true},
                {field: 'name', title: '轮播图名称'},
                {field: 'pic',  title: '轮播图', align: 'center', templet: "#carouselPic"},
                {field: 'url', title: '图片超链接', sort: true},
                {field: 'info',  title: '简介'},
                {field: 'state', title: '启用状态', align: 'center', templet: "#shelfStateSwitch", sort: true},
                {
                    title: '操作', minWidth: 150, toolbar: '#carouselTableBar', align: "center"
                }
            ]],
            limits: [4, 15, 20, 25, 50, 100],
            limit: 4,
            page: true,
            loading: true,
            skin: 'line',
        });

        /**
         * 触发表格复选框选择
         */
        table.on('checkbox(carouselTable)', function (obj) {
            console.log(obj)
        });

        /**
         * 表格头部工具条事件
         */
        table.on('toolbar(carouselTableFilter)', function (obj) {
            if (obj.event === 'addCarousel') {
                var index = layer.open({
                    title: '添加轮播图',
                    type: 2,
                    shade: 0.2,
                    offset: 'lt',
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/manager/carousel/openAddUpdate',
                    end: function () {
                        table.reload('carouselTable', {}, 'data');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            else if (obj.event === 'batchDelist') {
                layer.confirm('确定批量删除轮播图？', {icon: 3}, function(){
                    var id = obj.config.id;
                    var checkStatus = table.checkStatus(id);
                    var data = checkStatus.data;
                    var selectedIds = [];
                    for (var i = 0; i < data.length; i++) {
                        var row = data[i];
                        selectedIds.push(row.id); // 假设id字段为id，你需要将其替换为实际的id字段名
                    }
                    $.ajax({
                        url: '${pageContext.request.contextPath}/manager/carousel/batchDelete',
                        type: 'GET',
                        dataType: 'json',
                        data: {
                            ids: selectedIds.toString(),
                        },
                        success: function (data) {
                            layer.msg("批量删除成功");
                            //成功后重载数据
                            table.reload('carouselTable', {}, 'data');
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            alert('获取数据失败');
                        }
                    });
                }, function(){
                    layer.msg('取消删除');
                });
            }
        });

        /**
         * 启用状态开关事件
         */
        form.on('switch(Switch)', function (obj) {
            $.ajax({
                url: '${pageContext.request.contextPath}/manager/carousel/updateState',
                data: {
                    id: this.value
                },
                type: 'GET',
                success: function () {
                    layer.msg('更新成功');
                },
                error: function (xhr, textStatus, errorThrown) {
                    layer.msg('更新失败');
                }
            });
        });

        /**
         * 行内操作按钮事件
         */
        table.on('tool(carouselTableFilter)', function (obj) {
            var data = obj.data;
            /**
             * 删除
             */
            if (obj.event === 'delete') {
                layer.confirm('确定删除此轮播图？', {icon: 3}, function(){
                    $.ajax({
                        url: '${pageContext.request.contextPath}/manager/carousel/deleteById',
                        type: 'POST',
                        dataType: 'json',
                        data: {
                            id: data.id,
                        },
                        success: function (data) {
                            layer.msg("删除成功");
                            //成功后重载数据
                            table.reload('carouselTable', {}, 'data');
                        },
                        error: function () {
                            alert('获取数据失败');
                        }
                    });
                }, function(){
                    layer.msg('取消删除');
                });
            }
        });
    });
</script>
</body>
</html>
